#include "maininterface.h"
#include "ui_maininterface.h"

QTcpSocket * socket;

mainInterface::mainInterface(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::mainInterface)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_DeleteOnClose);

    QFile file(":/maininterface.qss"); // 假设style.qss位于资源文件中
    if (file.open(QFile::ReadOnly))
    {
        setStyleSheet(QString(file.readAll())); // 应用全局样式
        file.close();
    }

    setWindowFlag(Qt::FramelessWindowHint,true);//去掉主窗口的边框
    setAttribute(Qt::WA_TranslucentBackground,true);//把主窗口透明

    socket = new QTcpSocket(this);
    socket->connectToHost("192.168.32.188",9000);

    connect(this,SIGNAL(closeParent()),this->parent(),SLOT(close()));
    // s = new settings();
    // s->hide();
}

mainInterface::~mainInterface()
{
    qDebug()<<"mainInterface";
    delete socket;
    delete ui;
    emit closeParent();
    // delete s;
}

void mainInterface::on_closeButton_clicked()
{
//    QApplication* app;
//    app->exit(0);
    this->close();
}

void mainInterface::mousePressEvent(QMouseEvent *event)
{
    if(false == ui->settingsPushButton->underMouse() && false == ui->musicPushButton->underMouse() &&
        false == ui->weatherPushButton->underMouse() && false == ui->closeButton->underMouse())
        mouseOff = event->globalPos() - geometry().topLeft();
}

void mainInterface::mouseMoveEvent(QMouseEvent *event)
{
    if(false == ui->settingsPushButton->underMouse() && false == ui->musicPushButton->underMouse() &&
        false == ui->weatherPushButton->underMouse() && false == ui->closeButton->underMouse())
        this->move(event->globalPos() - mouseOff);
}

void mainInterface::on_settingsPushButton_clicked()
{
    settings * s = new settings(this);
    connect(s,SIGNAL(changeCity(QString)),ui->weather,SLOT(reRequest(QString)));
    s->show();
}

void mainInterface::on_musicPushButton_clicked()
{
     music * m = new music(this);
     m->show();
}


void mainInterface::on_weatherPushButton_clicked()
{
    weather * w = new weather(this);
    w->show();
}
